package com.blockmeta.bbs.immodule.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.utils.SharedPreferenceUtil;
import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.net.pojo.BaseImResponse;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.rxjava.CommonSubscriber;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.immodule.Constants;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.R2;
import com.blockmeta.bbs.immodule.adapter.JoinCircleRequestAdapter;
import com.blockmeta.bbs.immodule.bean.beaninterface.IJoinCircleRequestEntry;
import com.blockmeta.bbs.immodule.bean.databean.JoinCircleRequestBean;
import com.blockmeta.bbs.immodule.corebean.Impl.IMApplicationContext;
import com.blockmeta.bbs.immodule.dao.Todo;
import com.blockmeta.bbs.immodule.net.api.DealWithTodoApi;
import com.blockmeta.bbs.immodule.widget.Popup_Circle_Add_Request;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import butterknife.BindView;
import razerdp.basepopup.BasePopupWindow;

/**
 * @author airthor
 * @date 2019/4/4
 * 加圈子的请求
 */
public class MyAllTodoActivity extends BaseActivity {
    @BindView(R2.id.rcv_request_list)
    RecyclerView mRecyclerView;

    private JoinCircleRequestAdapter mCircleRequestAdapter;
    private List<IJoinCircleRequestEntry> mRequestEntries = new ArrayList<>();

    @Override
    protected boolean showTitle() {
        return true;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        IMApplicationContext.getIMMetaService().getTodoList();
    }

    @Override
    public View setView() {
        return getLayoutInflater().inflate(R.layout.activity_join_circle_requests,null);
    }

    @Override
    protected void initialize() {
        super.initialize();
        setTitleText("我的代办");

        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mCircleRequestAdapter = new JoinCircleRequestAdapter(R.layout.item_join_circle_request,mRequestEntries);
        ((SimpleItemAnimator) mRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        mRecyclerView.setAdapter(mCircleRequestAdapter);
        mCircleRequestAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if(view.getId() == R.id.btn_pass){

                    Popup_Circle_Add_Request popup= new Popup_Circle_Add_Request(mContext, mRequestEntries.get(position));
                    popup.setOnDismissListener(new BasePopupWindow.OnDismissListener() {
                        @Override
                        public void onDismiss() {
                            netWork();
                        }
                    });
                    popup.showPop();
                }else if(view.getId() == R.id.btn_refuse){
                    Popup_Circle_Add_Request popup= new Popup_Circle_Add_Request(mContext, mRequestEntries.get(position));
                    popup.setOnDismissListener(new BasePopupWindow.OnDismissListener() {
                        @Override
                        public void onDismiss() {
                            netWork();
                        }
                    });
                    popup.showPop();
                }else if(view.getId() == R.id.btn_delete){
                    dealWithTodoAndDelete(mRequestEntries.get(position));
                }
            }
        });

        if (mCircleRequestAdapter.getEmptyView() == null){
            View emptyView = getLayoutInflater().inflate(R.layout.empty_login,null);
            ImageView icon = emptyView.findViewById(R.id.iv_icon);
            icon.setImageResource(R.drawable.icon_empty_list);
            TextView textView = emptyView.findViewById(R.id.empty_chat);
            textView.setText("暂无请求");
            mCircleRequestAdapter.setEmptyView(emptyView);
        }
        netWork();
    }

    private void dealWithTodoAndDelete(final IJoinCircleRequestEntry todo) {
        if(!todo.isCompleted()){
            RetrofitFactory.getRetrofit().create(DealWithTodoApi.class)
                    .dealWithTodo(AccountHelper.getUidInt(),todo.getTodoId(), false, "拒绝")
                    .compose(RxUtil.dataNullResult())
                    .compose(this.<BaseImResponse<Object>>bindToLifecycle())
                    .compose(RxUtil.<BaseImResponse<Object>>rxSchedulerHelper())
                    .subscribe(new CommonSubscriber<BaseImResponse<Object>>(mContext) {
                        @Override
                        public void onNext(BaseImResponse<Object> o) {
                            IMApplicationContext.getTodoDomainService().deleteTodo(todo.getTodoId());
                        }
                    });

        }else {
            IMApplicationContext.getTodoDomainService().deleteTodo(todo.getTodoId());
        }
    }

    /**
     * 获取加圈请求列表
     */
    private void netWork() {
        mRequestEntries.clear();
        final List<Todo> todos= IMApplicationContext.getTodoDomainService().getAllTodoList(AccountHelper.getUidInt());
        for (final Todo todo : todos) {
            final JoinCircleRequestBean bean = new JoinCircleRequestBean();
            bean.setTodoId(todo.getTodoId());
            bean.setFromId(todo.getFromId());
            bean.setPassed(todo.getAgree());
            bean.setRequestMsg(todo.getRequestMsg());
            bean.setComplete(todo.getComplete());
            bean.setGroupId(todo.getGroupId());
            mRequestEntries.add(bean);
        }
        mCircleRequestAdapter.notifyDataSetChanged();

        recordLasteastTodoTime(todos);
    }

    private void recordLasteastTodoTime(List<Todo> todos) {
        if(todos != null && todos.size() > 0){
            SharedPreferenceUtil.putLong(mContext, Constants.CIRCLE_LASTTODO_TIME, todos.get(0).getCreateDate());
        }
    }

}
